<template>

  <van-nav-bar left-text="返回" left-arrow @click-left="router.go(-1)"/>

  <van-address-list
      v-model="chosenAddressId"
      :list="addressList"
      default-tag-text="默认"
      @add="handleAdd"
      @edit="handleUpdate"
  />
</template>


<script setup lang="ts">
import {ref, reactive} from 'vue';
import {getAddressList} from "@/api/address"
import {useRouter} from 'vue-router';

const router = useRouter();

let addressList=reactive([]);

//默认选中的地址id
const chosenAddressId = ref('1');

const loadAddressList=()=>{
  getAddressList().then((resp)=>{
    addressList.length = 0;
    if(resp.data){
      for(let oriAddressInfo of resp.data){
        let address = {};
        address.id = oriAddressInfo.id;
        address.name = oriAddressInfo.consignee;
        address.tel = oriAddressInfo.phone;

        address.province = oriAddressInfo.provinceName;
        address.city = oriAddressInfo.cityName;
        address.county = oriAddressInfo.areaName;

        address.address = oriAddressInfo.detail;
        address.isDefault = oriAddressInfo.isDefault;

        addressList.push(address);
      }
    }
  });
}

loadAddressList();

const handleAdd = () => {
  router.push({"path":"/address/save"});
};

const handleUpdate = (item, index) => {
  router.push({"path":"/address/save","query":{"id":item.id}});
};
 
</script>

<style scoped>
</style>